/**
 * 
 */
package com.lanyou.esb.servicemix.monitor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.lanyou.cook.common.Constants;
import com.lanyou.cook.entity.domain.ParamSetting;
import com.lanyou.cook.paramsetting.ParamSettingReppository;
import com.lanyou.cook.util.gson.GsonUtil;
import com.lanyou.esb.servicemix.monitor.rest.MonitorService;
import com.lanyou.esb.servicemix.monitor.rest.ProxyService;
import com.lanyou.esb.servicemix.monitor.rest.RestFactory;

/**
 * @author gonglei
 *
 */
@Service
public class ESBServerFactory {

	private static ESBServerFactory esbServerFactory;

	@Autowired
	private ParamSettingReppository paramSettingReppository;

	private static List<ESBServer> servers = new ArrayList<>();

	public static ESBServerFactory getInstance() {
		if (esbServerFactory == null) {
			esbServerFactory = new ESBServerFactory();
		}
		return esbServerFactory;
	}

	public List<ESBServer> getESBServer() {
		if (servers.isEmpty()) {
			initServer();
		}
		return servers;
	}

	/**
	 * 根据IP获取服务
	 * 
	 * @param ip
	 * @return
	 */
	public ESBServer getESBServer(String ip) {
		ParamSetting paramSettingServer = paramSettingReppository
				.findByParamName(Constants.SERVERMIX_SERVER);
		if (paramSettingServer == null) {
			return null;
		}
		JsonArray jsonDatas = GsonUtil.getDefaultJsonParser()
				.parse(paramSettingServer.getParamValue()).getAsJsonArray();
		try {
			for (JsonElement jsonElement : jsonDatas) {
				if (!jsonElement.getAsJsonObject().get("ip").getAsString()
						.equals(ip)) {
					continue;
				}
				ESBServer esbServer = new ESBServer();
				esbServer.setServerAddres(ip);
				esbServer.setPort(Integer.parseInt(jsonElement
						.getAsJsonObject().get("port").getAsString()));

				RestFactory restFactory = new RestFactory(esbServer);
				esbServer.setRestFactory(restFactory);
				return esbServer;
			}
		} catch (Exception e) {
			return null;
		}
		return null;
	}

	private synchronized void initServer() {
		if (!servers.isEmpty()) {
			return;
		}
		ParamSetting paramSettingServer = paramSettingReppository
				.findByParamName(Constants.SERVERMIX_SERVER);
		// TODO
		if (paramSettingServer == null) {
			return;
		}
		JsonArray jsonDatas = GsonUtil.getDefaultJsonParser()
				.parse(paramSettingServer.getParamValue()).getAsJsonArray();
		for (JsonElement jsonElement : jsonDatas) {

			ESBServer esbServer = new ESBServer();
			// esbServer.setServerCode(paramSettingServer.getParamName());
			esbServer.setServerAddres(jsonElement.getAsJsonObject().get("ip")
					.getAsString());
			esbServer.setPort(Integer.parseInt(jsonElement.getAsJsonObject()
					.get("port").getAsString()));

			RestFactory restFactory = new RestFactory(esbServer);
			esbServer.setRestFactory(restFactory);
			servers.add(esbServer);
		}

	}

	public List<MonitorService> getMonitorService() {
		List<MonitorService> list = new ArrayList<>();
		List<ESBServer> esbServers = getESBServer();
		if (esbServers == null) {
			return new ArrayList<>(0);
		}
		for (ESBServer server : esbServers) {
			list.add(server.getRestFactory().getMonitorService());
		}
		return list;
	}

	public List<ProxyService> getProxyService() {
		List<ProxyService> list = new ArrayList<>();
		List<ESBServer> esbServers = getESBServer();
		if (esbServers == null) {
			return new ArrayList<>(0);
		}
		for (ESBServer server : esbServers) {
			list.add(server.getRestFactory().getProxyService());
		}
		return list;
	}

	/*
	 * public static void main(String[] args) { String str =
	 * "[{\"ip\":\"172.28.16.89\",\"port\":\"8181\"},{\"ip\":\"172.28.16.90\",\"port\":\"8181\"},{\"ip\":\"172.28.16.91\",\"port\":\"8181\"}]"
	 * ; JsonArray jsonDatas =
	 * GsonUtil.getDefaultJsonParser().parse(str).getAsJsonArray();
	 * ESBServerFactory esbServerFactory = new ESBServerFactory();
	 * esbServerFactory.getESBServer(); System.out.println("fsdf"); }
	 */
}
